package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.DigitalSliceCaseDifferentialDiagnosisMapper;
import com.zhijian.medical.entity.po.DigitalSliceCase;
import com.zhijian.medical.entity.po.DigitalSliceCaseDifferentialDiagnosis;
import com.zhijian.medical.entity.vo.request.DigitalSliceCaseDifferentialDiagnosisSearchVo;
import com.zhijian.medical.entity.vo.response.DigitalSliceCaseDifferentialDiagnosisVo;
import com.zhijian.medical.service.DigitalSliceCaseDifferentialDiagnosisService;
import com.zhijian.medical.service.DigitalSliceCaseService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 数字切片-病例-鉴别诊断关联业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-01-04 21:26:38
 */
@Slf4j
@Service
public class DigitalSliceCaseDifferentialDiagnosisServiceImpl implements DigitalSliceCaseDifferentialDiagnosisService {

    @Autowired
    private DigitalSliceCaseDifferentialDiagnosisMapper mapper;

    @Autowired
    private DigitalSliceCaseService digitalSliceCaseService;

    @Override
    public DigitalSliceCaseDifferentialDiagnosis selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DigitalSliceCaseDifferentialDiagnosisVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(DigitalSliceCaseDifferentialDiagnosis digitalSliceCaseDifferentialDiagnosis) {
        Date now = new Date();
        digitalSliceCaseDifferentialDiagnosis.setCreateTime(now);
        digitalSliceCaseDifferentialDiagnosis.setUpdateTime(now);
        return mapper.insert(digitalSliceCaseDifferentialDiagnosis) > 0 ? digitalSliceCaseDifferentialDiagnosis.getId() : 0L;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public List<DigitalSliceCaseDifferentialDiagnosisVo> saveAll(DigitalSliceCaseDifferentialDiagnosis insertParam) {
        //主病例
        DigitalSliceCase mainDigitalSliceCase = digitalSliceCaseService.selectById(insertParam.getCaseId());
        AssertUtil.notNull(mainDigitalSliceCase, "此病例不存在或者已被删除");
        //关联病例
        DigitalSliceCase relationDigitalSliceCase = digitalSliceCaseService.selectByCaseId(insertParam.getRelationRealCaseId());
        AssertUtil.notNull(relationDigitalSliceCase, "关联病例不存在或者已被删除");
        AssertUtil.check(Objects.equals(insertParam.getCaseId(), relationDigitalSliceCase.getId()), "不允许关联自身");
        //先判断下是否有重复的鉴别诊断
        Integer count = this.count(DigitalSliceCaseDifferentialDiagnosisSearchVo.builder()
                .caseId(insertParam.getCaseId())
                .differentialDiagnosis(insertParam.getDifferentialDiagnosis())
                .build());
        AssertUtil.check(count > 0, "存在重复的数据，请重新输入");
        count = this.count(DigitalSliceCaseDifferentialDiagnosisSearchVo.builder()
                .caseId(insertParam.getCaseId())
                .relationCaseId(relationDigitalSliceCase.getId())
                .build());
        AssertUtil.check(count > 0, "存在重复的数据，请重新输入");
        insertParam.setRelationCaseId(relationDigitalSliceCase.getId());
        this.insert(insertParam);
        //还要保存一个相反的对应关系
        DigitalSliceCaseDifferentialDiagnosis s = new DigitalSliceCaseDifferentialDiagnosis();
        s.setCaseId(insertParam.getRelationCaseId());
        s.setRelationCaseId(insertParam.getCaseId());
        s.setRelationRealCaseId(mainDigitalSliceCase.getCaseId());
        s.setDifferentialDiagnosis(mainDigitalSliceCase.getPathologicDiagnosis());
        this.insert(s);
        return this.selectList(DigitalSliceCaseDifferentialDiagnosisSearchVo.builder()
                .caseId(insertParam.getCaseId())
                .build());
    }

    @Override
    public boolean update(DigitalSliceCaseDifferentialDiagnosis digitalSliceCaseDifferentialDiagnosis) {
        Date now = new Date();
        digitalSliceCaseDifferentialDiagnosis.setUpdateTime(now);
        return mapper.updateByPrimaryKey(digitalSliceCaseDifferentialDiagnosis) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(DigitalSliceCaseDifferentialDiagnosis record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(DigitalSliceCaseDifferentialDiagnosisSearchVo digitalSliceCaseDifferentialDiagnosisSearchVo) {
        return mapper.count(digitalSliceCaseDifferentialDiagnosisSearchVo);
    }

    @Override
    public List<DigitalSliceCaseDifferentialDiagnosisVo> selectList(DigitalSliceCaseDifferentialDiagnosisSearchVo digitalSliceCaseDifferentialDiagnosisSearchVo) {
        return mapper.selectList(digitalSliceCaseDifferentialDiagnosisSearchVo);
    }

    @Override
    public Page<DigitalSliceCaseDifferentialDiagnosisVo> selectPage(DigitalSliceCaseDifferentialDiagnosisSearchVo digitalSliceCaseDifferentialDiagnosisSearchVo) {
        //先直接查总数
        int count = count(digitalSliceCaseDifferentialDiagnosisSearchVo);
        //分页
        Page<DigitalSliceCaseDifferentialDiagnosisVo> page = new Page<>(true, count, digitalSliceCaseDifferentialDiagnosisSearchVo.getPageNum(), digitalSliceCaseDifferentialDiagnosisSearchVo.getPageSize());
        digitalSliceCaseDifferentialDiagnosisSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DigitalSliceCaseDifferentialDiagnosisVo> responseVos = this.selectList(digitalSliceCaseDifferentialDiagnosisSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<DigitalSliceCaseDifferentialDiagnosis> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

}